from http import HTTPStatus

import pytest


@pytest.fixture
def client_json():
    return {
        "name": "Ivan",
        "surname": "Ivanov",
        "credit_card": "1234123412341234",
        "car_number": "A000AA98",
    }


def test_create_client_route(client, client_json):
    response = client.post("/clients/", json=client_json)
    assert response.status_code == HTTPStatus.CREATED
    assert response.json == {"id": response.json["id"], **client_json}


@pytest.mark.parametrize(
    "wrong_json",
    [
        {
            "name": "Ivan",
            "surname": "Ivanov",
            "credit_card": "1234123412341234",
        },
        {
            "name": "Ivan",
            "surname": "Ivanov",
            "car_number": "A000AA98",
        },
        {
            "name": "Ivan",
            "credit_card": "1234123412341234",
            "car_number": "A000AA98",
        },
        {
            "surname": "Ivanov",
            "credit_card": "1234123412341234",
            "car_number": "A000AA98",
        },
    ],
)
def test_create_client_route_missing_fields(client, wrong_json):
    response = client.post("/clients/", json=wrong_json)
    assert response.status_code == HTTPStatus.BAD_REQUEST
    assert response.json.get("error") == "All client fields are required"


def test_get_clients_list_route(client, client_json):
    response = client.get("/clients/")
    assert response.status_code == HTTPStatus.OK
    assert len(response.json) == 1
    assert response.json[0] == {"id": 1, **client_json}


def test_get_client_route(client, client_json):
    response = client.get("/clients/1")
    assert response.status_code == HTTPStatus.OK
    assert response.json == {"id": 1, **client_json}


def test_get_client_route_with_invalid_id(client):
    response = client.get("/clients/123")
    assert response.status_code == HTTPStatus.NOT_FOUND
    assert "does not exist" in response.json.get("error")
